$namespace("PP.index");
PP.index.init=function(){
	this.focusSearch();
	this.bindPush();
	this.bindTabs();
	this.bindSort();
	this.hideBanner();
	this.bindColor();
	this.bindShop();
	this.bindInfo();
	this.loadImg();
	this.bindSale();
	this.bindCharge();
	this.bindCount();
	this.showTips();
	this.bindWebQQ();
};
PP.index.focusSearch=function(){
	$addEvent(window,"load",function(){
		$id("KeyWord").focus();
	});
};
PP.index.bindPush=function(){
	$slider({
		auto:true,
		width:470,
		effect:"scrollx",
		initIndex:0,
		titleId:"sliderNum",
		titleTag:"li",
		contentId:"sliderImg",
		contentTag:"li",
		funcTabInit:function(i,dom){
			var oLink=this.cont[i].firstChild,
			oImg=oLink.firstChild;dom.innerHTML='<a href="'+oLink.href+'" target="_blank">'+oImg.getAttribute("alt")+'</a>';
		}
	});
};
PP.index.bindTabs=function(){
	$slider({titleId:"notice",titleTag:"li",contentId:"noticeContents",contentTag:"ul",initIndex:0});
	$slider({titleId:"brandTab",titleTag:"li",contentId:"brandCnt",contentTag:"ul",initIndex:0});
};
PP.index.bindSort=function(){
	var _conts=[],
	oList=$id("categoryList");
	_conts=$child(oList,"div");
	for(var i=0,len=_conts.length;i<len;i++){
		_conts[i].onmouseover=function(){
			this.className=this.className+" hover";
		};
		_conts[i].onmouseout=function(){
			this.className=this.className.replace(" hover","");
		}
	}
};
PP.index.hideBanner=function(){
	$slider({
		contentId:"hide15sec",
		contentTag:"a"
	});
	setTimeout(function(){
		$id("hide15sec").style.display="none";
	},"15000");
};
PP.index.bindColor=function(){
	var nowDay=new Date().getDay();
	$slider({
		titleId:'colorTitle',
		titleTag:'a',
		contentId:"colorList",
		contentTag:"li",
		initIndex:nowDay==6?0:nowDay
	});
};
PP.index.bindShop=function(){
	$slider({
		contentId:"qqshopList",
		contentTag:"li",
		prevId:"shopPrevLink",
		nextId:"shopNextLink",
		auto:true,
		effect:"fade",
		initIndex:0,
		autoLag:8000
	});
};
PP.index.bindInfo=function(){
	$slider({
		contentId:"infoList",
		contentTag:"li",
		prevId:"infoPrevLink",
		nextId:"infoNextLink",
		auto:true,
		effect:"fade",
		initIndex:0
	});
};
PP.index.bindCharge=function(){
	$id("chargeIndex").src="http://www.paipai.com/optimal/charge_v1.2.shtml"
};
PP.index.loadImg=function(){
	$scroll({
		id:"infoList",
		func:function(opt){
			$loadImg($id(opt.id),"init_src");
		}}
	);
	$scroll({
		id:"bbsLazy",
		func:function(opt){
			$loadImg($id(opt.id),"init_src");
		}}
	);
	$scroll({
		id:"footPushLazy",
		func:function(opt){
			$loadImg($id(opt.id));
		}
	});
	$scroll({
		id:"hotgoodsLazy",
		func:function(opt){
			$loadImg($id(opt.id),"init_src");
		}
	});
	$scroll({
		id:"sidePushLazy",
		func:function(opt){
			$loadImg($id(opt.id));
		}
	});
};
PP.index.bindSale=function(){
	var timeList={},
	total=0,
	firstTime=0,
	secondTime=0,
	endTime=0,
	sl=$slider({
		initIndex:0,
		isLoadInit:false,
		perView:3,
		prevId:"salePrevLink",
		nextId:"saleNextLink",
		contentId:"entryItem",
		contentTag:"li",
		funcInit:function(now,dom){
			var itemTime=dom.getAttribute('uploadTime');
			timeList[itemTime]=now;
			now==0&&(firstTime=itemTime);
			now==1&&(secondTime=itemTime);
			endTime=itemTime;
			total=now+1;
			}
	});
	if(endTime==0){return null;};
	var nowTime=$formatDate(new Date(),"HH:II").split(":"),
		firstTime=firstTime.split(":"),
		secondTime=secondTime.split(":"),
		endTime=endTime.split(":"),
		per=secondTime[1]*1-firstTime[1]*1,
		nowHours=nowTime[0]*1,
		nowMin=Math.ceil(nowTime[1]*1/per)*per,
		endHours=endTime[0]*1,
		endMin=endTime[1]*1,
		totalPage=Math.ceil(total/3),
		curPage=0;
	if(nowMin>=60){
		nowMin=0;
		nowHours++;
	};
	if(nowHours>endHours||(nowHours===endHours&&nowMin>=endMin)){
		curPage=totalPage;
	}else if(nowHours>=firstTime[0]*1){
		var zeroHours=$addZero(nowHours,2),
			curTime=zeroHours+":"+$addZero(nowMin,2),
			order=timeList[curTime];
		undefined===order&&(order=timeList[zeroHours+":00"]);
		undefined!==order&&(curPage=Math.ceil((order+1)/3)-1);
	};
	sl.focusIndex(curPage);
};
PP.index.bindCount=function(){
	$addEvent(document.body,"click",function(){
		pgvWatchClick()
	});
};
PP.index.showTips=function(){
	if($getCookie("indexAd")){return;}
	var nowDate=new Date(),
		nowHour=nowDate.getHours(),
		nowMint=nowDate.getMinutes(),
		time=60*(24-nowHour)-nowMint,
		adTips0="block",
		adTips1="block",
		html=$id("adTips").innerHTML,
		mp=!!$getMp(10);
	$setCookie('indexAd',"1",time,'/','paipai.com');
	var adTip=$adFloater({
		html:html,
		closerId:"adTipsCloser",
		func:function(opt){
			var oView=$id(mp?"adTips0":"adTips1");
			oView.style.display="block";
			$loadImg(oView);
		}
	});
};
PP.index.bindWebQQ=function(){
	$addEvent(document.body,"mousedown",function(e){
		var tar=$getTarget(e,document.body,"a");
		if(tar){}
	});
};
function $id(id){
	return typeof(id)=="string"?document.getElementById(id):id;
};
function $namespace(str){
	var arr=str.split(',');
	for(var i=0,len=arr.length;i<len;i++){
		var arrJ=arr[i].split("."),
			parent={};
		for(var j=0,jLen=arrJ.length;j<jLen;j++){
			var name=arrJ[j],
				child=parent[name];
			j===0?eval('(typeof '+name+')==="undefined"?('+name+'={}):"";parent='+name)
					:(parent=parent[name]=(typeof child)==='undefined'?{}
					:child);
		};
	}
};
function $getY(e){
	var t=e.offsetTop;
	while(e=e.offsetParent){
		t+=document.body!=e?e.offsetTop:0;
	}
	return t;
}
function $formatDate(date,formatStr){
	var arrWeek=['日','一','二','三','四','五','六'],
		str=formatStr.replace(/yyyy|YYYY/,date.getFullYear())
			.replace(/yy|YY/,$addZero(date.getFullYear()%100,2))
			.replace(/mm|MM/,$addZero(date.getMonth()+1,2))
			.replace(/m|M/g,date.getMonth()+1)
			.replace(/dd|DD/,$addZero(date.getDate(),2))
			.replace(/d|D/g,date.getDate())
			.replace(/hh|HH/,$addZero(date.getHours(),2))
			.replace(/h|H/g,date.getHours())
			.replace(/ii|II/,$addZero(date.getMinutes(),2))
			.replace(/i|I/g,date.getMinutes())
			.replace(/ss|SS/,$addZero(date.getSeconds(),2))
			.replace(/s|S/g,date.getSeconds())
			.replace(/w/g,date.getDay())
			.replace(/W/g,arrWeek[date.getDay()]);
	return str;
};
function $setCookie(name,value,expiresd,path,domain,secure){
	var expdate=new Date(),
		expires=arguments[2]||null,
		path=arguments[3]||"/",
		domain=arguments[4]||null,
		secure=arguments[5]||false;
	expires?expdate.setMinutes(expdate.getMinutes()+parseInt(expires)):"";
	var cookietemp=escape(name)+'='+escape(value)+(expires?'; expires='+expdate.toGMTString():'')+(path?'; path='+path:'')+(domain?'; domain='+domain:'')+(secure?'; secure':'');
	document.cookie=cookietemp;
};
function $addEvent(obj,type,handle){
	if(window.addEventListener){
		obj.addEventListener(type,handle,false);
	}else if(window.attachEvent){
		obj.attachEvent("on"+type,handle);
	}else{
		obj["on"+type]=handle;
	}
};
function $addClass(ids,cName){
	$setClass(ids,cName,"add");
};
function $delClass(ids,cName){
	$setClass(ids,cName,"remove");
};
function $getCookie(name){
	var reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"),
		val=document.cookie.match(reg);
	return val?unescape(val[2]):null;
};
function $setClass(ids,cName,kind){
	if(typeof(ids)=="string"){
		var arrDom=ids.split(",");
		for(var i=0,len=arrDom.length;i<len;i++){
			setClass($id(arrDom[i]),cName,kind);
		}
	}else{
		setClass(ids,cName,kind);
	};
	function setClass(obj,cName,kind){
		var oldName=obj.className,
			arrName=oldName?oldName.split(' '):[];
		if(kind=="add"){
			if(!$hasClass(oldName,cName)){
				arrName.push(cName);
				obj.className=arrName.join(' ');
			}
		}else if(kind=="remove"){
			var newName=[];
			for(var i=0,len=arrName.length;i<len;i++){
				if(cName!=arrName[i]&&' '!=arrName[i]){
					newName.push(arrName[i]);
				}
			};
			obj.className=newName.join(' ');
		}
	}
};
function $delEvent(obj,type,handle){
	if(window.removeEventListener){
		obj.removeEventListener(type,handle,false);
	}else if(window.detachEvent){
		obj.detachEvent("on"+type,handle);
	}
};
(function(){
	var _adFloaterHandle=null;
	$adFloater=function(obj){
		var option={
				html:"",
				right:1,
				bottom:1,
				fix:true,
				closerId:"",
				id:"adFloater",
				className:"adfloater",
				func:$empty()
			};
		for(var i in obj){
			option[i]=obj[i];
		}
		if(_adFloaterHandle){
			try{
				_adFloaterHandle.close();
				_adFloaterHandle=null;
			}catch(e){}
		}
		var c=document.createElement("div");
		c.id=option.id;
		c.className=option.className;
		c.style.position="absolute";
		c.innerHTML='<div id="'+option.id+'Content">'+option.html+'</div><div id="'
					+option.id+'Cover" style="width:0px; height:0px; position:absolute; top:0px; left:0px; z-index:-1;filter:Alpha(Opacity=0,Style=0);opacity:0;"><iframe id="'
					+option.id+'Iframe" frameborder="0" scrolling="no" style=" " height="0"   width="0"> </div>';
		document.body.appendChild(c);
		option.content=$id(option.id+"Content");
		option.cover=$id(option.id+"Cover");
		option.iframe=$id(option.id+"Iframe");
		option.cover.style.height=option.iframe.style.height=c.scrollHeight+"px";
		option.cover.style.width=option.iframe.style.width=c.scrollWidth+"px";
		option.frame=c;
		option.frame.style.zIndex="11111";
		if(option.fix){
			if(navigator.appVersion.split(";")[1].replace(/[ ]/g,"")=="MSIE6.0"){
				setInterval(function(){
					if(option.frame){
						option.frame.style.left=Number(document.documentElement.scrollLeft)
												+(Number(document.documentElement.clientWidth)-Number(option.frame.scrollWidth)-option.right)+"px";
						option.frame.style.top=Number(document.documentElement.scrollTop)
												+(Number(document.documentElement.clientHeight)-Number(option.frame.scrollHeight)-option.bottom)+"px";
					}
					var c=parseInt(option.frame.style.top.replace("px",""));
					var o=parseInt(option.frame.getAttribute("oldTop"));
					(!o)?option.frame.setAttribute("oldTop",c):"";
					option.frame.setAttribute("oldTop",c);
				},40);
			}else{
				option.frame.style.position="fixed";
				option.frame.style.right=option.right+"px";
				option.frame.style.bottom=option.bottom+"px";
				option.frame.style.left="";
				option.frame.style.top="";
			}
		}
		option.close=function(){
			option.frame.parentNode.removeChild(option.frame);
			delete option;
		};
		if(option.closerId){
			$id(option.closerId).onclick=option.close;
		};
		option.func(option);
		_adFloaterHandle=option;
		return option;
	}
})()
function $randomInt(num){
	return Math.floor(Math.random()*num);
};
function $hasClass(old,cur){
	if(!old||!cur)
		return null;
	var arr=old.split(' ');
	for(var i=0,len=arr.length;i<len;i++){
		if(cur==arr[i]){
			return cur;
		}
	};
	return null;
}
(function(){
	var isBind=false,
		heightList=[],
		funcList=[],
		optList=[],
		visibleH=document.documentElement.clientHeight;
	$scroll=function(opt){
		var height=opt.height?opt.height:$getY($id(opt.id));
		if(visibleH<height){
			heightList.push(height*1);
			funcList.push(opt.func);
			optList.push(opt);
		}else{
			opt.func(opt);
		};
		if(isBind){
			isBind=true;
		}else{
			$addEvent(window,'scroll',doScroll);
			$addEvent(window,'resize',doResize);
		};
		function doResize(){
			visibleH=document.documentElement.clientHeight;
			doScroll();
		};
		function doScroll(){
			var len=heightList.length;
			if(len===0){
				$delEvent(window,'scroll',doScroll);
				$delEvent(window,'resize',doScroll);
				return null;
			};
			var dv=document.defaultView,
				y=(dv)?dv.pageYOffset:0,
				h=Math.max(document.body.scrollTop,document.documentElement.scrollTop,y)+visibleH,
				arrHeight=[],
				arrFunc=[],
				arrOpt=[];
			for(var i=0;i<len;i++){
				if(h>heightList[i]){
					funcList[i](optList[i]);
				}else{
					arrHeight.push(heightList[i]);
					arrFunc.push(funcList[i]);
					arrOpt.push(optList[i]);
				}
			};
			heightList=arrHeight;
			funcList=arrFunc;
			optList=arrOpt;
		}
	};
})()
function $empty(){
	if(!window["_PP_core_empty"]){
		window["_PP_core_empty"]=function(){
			return true;
		}
	}
	return window["_PP_core_empty"];
}
function $addZero(v,size){
	for(var i=0,len=size-(v+"").length;i<len;i++){
		v="0"+v;
	};
	return v;
}
function $getTarget(e,parent,tag){
	var e=window.event||e,
		tar=e.srcElement||e.target;
	if(parent&&tag&&tar.nodeName.toLowerCase()!=tag){
		while(tar=tar.parentNode){
			if(tar==parent||tar==document.body||tar==document){
				return null;
			}else if(tar.nodeName.toLowerCase()==tag){
				break;
			}
		}
	};
	return tar;
}
function $child(node,val,fn){
	var results=[],
		node=node||document.body;
	walk(node.firstChild,function(n){
		var actual=n.nodeType===1&&n.nodeName.toLowerCase();
		if(typeof actual==='string'&&(actual===val||typeof val!=='string')){
			results.push(n);
			fn&&fn(n);
		}
	});
	return results;
	function walk(n,func){
		func(n);
		while(n=n.nextSibling){
			func(n,func);
		}
	}
}
function $getMp(n){
	var mp=$getCookie("mp");
	return mp?mp.split(":")[n]:"";
}
function $slider(obj){
	var opt={
			titleId:"",
			titleTag:"",
			contentId:"",
			contentTag:"",
			prevId:"",
			nextId:"",
			perView:1,
			className:"current",
			eventType:"mouseover",
			initIndex:NaN,
			timeLag:300,
			funcInit:$empty(),
			funcTabInit:$empty(),
			func:$empty(),
			auto:false,
			autoKeep:true,
			autoTimes:100,
			autoLag:5000,
			fadeLag:30,
			fadeTimes:300,
			effect:'none',
			width:0,
			height:0,
			backAttr:"back_src",
			isLoadInit:true,
			focusIndex:setEffect,
			clearAuto:function(){
				clearInterval(autoIntr)
			},
			cont:null,
			tabs:null
		};
	for(var i in obj){
		opt[i]=obj[i]
	};
	((opt.width==0&&opt.effect=="scrollx")||(opt.height==0&&opt.effect=="scrolly"))&&(opt.effect="none");
	var total=0,
		autoCount=0,
		isInit=true,
		intr=null,
		autoIntr=null,
		_imgs=[];
	if(opt.contentId){
		var oContent=$id(opt.contentId),
			_cont=$child(oContent,opt.contentTag,function(dom){
				switch(opt.effect){
					case"none":
						dom.style.display="none";
						break;
					case"scrollx":
						dom.style.width=opt.width+"px";
						dom.style.styleFloat=dom.style.cssFloat="left";
						break;
					case"scrolly":
						dom.style.height=opt.height+"px";
						break;
					case"fade":
						dom.style.display="none";
						dom.style.position="absolute";
						dom.style.left=0;
						dom.style.top=0;
						break;
				};
				opt.funcInit(total++,dom);
			});
		if(opt.auto){
			$addEvent(oContent,"mouseover",function(){
				clearInterval(autoIntr);
			});
			opt.autoKeep&&$addEvent(oContent,"mouseout",function(){
				setAuto();
			});
		};
		opt.cont=_cont;
	}
	var len=opt.perView,
		now=0;
	if(opt.titleId){
		var oTitle=$id(opt.titleId),
			_tabs=$child(oTitle,opt.titleTag,function(dom){
				opt.funcTabInit(now,dom);
				dom.setAttribute("index",now++);
			});
		$addEvent(oTitle,opt.eventType,function(e){
			var tar=$getTarget(e,oTitle,opt.titleTag);
			if(tar){
				var cur=tar.getAttribute("index")*1;
				clearInterval(autoIntr);
				if(cur!=current){
					intr=setTimeout(function(){
							setEffect(cur);
						},opt.timeLag);
				}
			}
		});
		$addEvent(oTitle,"mouseout",function(e){
			var tar=$getTarget(e,oTitle,opt.titleTag);
			if(tar){
				clearTimeout(intr);
				opt.auto&&opt.autoKeep&&setAuto();
			}
		});
		opt.tabs=_tabs;
		total=now;
	};
	var pageTotal=Math.ceil(total/len),
		current=isNaN(opt.initIndex)?$randomInt(pageTotal):opt.initIndex,
		autoTotal=opt.autoTimes*total-1;
	setEffect(current);
	opt.auto&&setAuto();
	opt.prevId&&($id(opt.prevId).onclick=function(){
		clearInterval(autoIntr);
		setEffect((now=current-1)<0?(pageTotal-1):now);
	});
	opt.nextId&&($id(opt.nextId).onclick=function(){
		clearInterval(autoIntr);
		setEffect((now=current+1)>=pageTotal?0:now);
	})
	isInit=false;
	return opt;
	function setAuto(){
		autoIntr=setInterval(function(){
			if(autoCount>=autoTotal){
				clearInterval(autoIntr);
			}else{
				setEffect((now=current+1)>=pageTotal?0:now);
				autoCount++;
			}
		},opt.autoLag);
	}
	function setEffect(cur){
		if(!opt.contentId){
			showItem(cur);
			current=cur;
			return;
		};
		if(isInit){
			switch(opt.effect){
				case"scrollx":
					oContent.style.position="relative";
					oContent.style.width=total*opt.width+"px";
					oContent.style.left=-current*opt.width+"px";
					break;
				case"scrolly":
					oContent.style.position="relative";
					oContent.style.top=-current*opt.height+"px";
					break;
				case"fade":
					oContent.style.position="relative";
					break;
			};
			for(var i=0;i<len;i++){
				(now=cur+i)<total&&showItem(now);
			};
			current=cur;
		}else{
			var fadeStep=Math.floor(opt.fadeTimes/opt.fadeLag),
				fadeIntr=null,
				fadeCount=0;
			opt.titleId&&$delClass(_tabs[current],opt.className);
			switch(opt.effect){
				case"none":
					for(var i=0;i<len;i++){
						(now=current*len+i)<total&&(_cont[now].style.display="none");
						(now=cur*len+i)<total&&showItem(now);
					};
					current=cur;
					break;
				case"scrollx":
					var left=getSpeed(opt.width);
					showItem(cur);
					fadeIntr=setInterval(function(){
						if(fadeCount++>=fadeStep){
							clearInterval(fadeIntr);
							current=cur;
						}else{
							oContent.style.left=-getMove(left)+"px";
						};
					},opt.fadeLag);
					break;
				case"scrolly":
					var top=getSpeed(opt.height);
					showItem(cur);
					fadeIntr=setInterval(function(){
						if(fadeCount++>=fadeStep){
							clearInterval(fadeIntr);
							current=cur;
						}else{
							oContent.style.top=-getMove(top)+"px";
						};
					},opt.fadeLag);
					break;
				case"fade":
					showItem(cur);
					fadeIntr=setInterval(function(){
						if(fadeCount++>=fadeStep){
							clearInterval(fadeIntr);
							_cont[current].style.zIndex=0;
							_cont[cur].style.zIndex=1;
							current=cur;
						}else{
							var per=fadeCount/fadeStep;
							_cont[current].style.filter="alpha(opacity="+(1-per)*100+")";
							_cont[current].style.opacity=1-per;
							_cont[cur].style.filter="alpha(opacity="+(per*100)+")";
							_cont[cur].style.opacity=per;
						};
					},opt.fadeLag);
					break;
			};
		}
		function getSpeed(s){
			var v=Math.ceil(s/fadeStep)+100,
				a=(v*fadeStep-s)/fadeStep/fadeStep,
				flag=(cur-current)<0?-1:1,
				end=cur*s,
				here=(cur-flag)*s,
				oFirst=_cont[0];
			current==0&&(oFirst.style.position="static");
			if(current+1==total&&cur==0){
				flag=1;
				end=(current+1)*s;
				here=current*s;
				oFirst.style.position="relative";
				opt.effect=="scrollx"?oFirst.style.left=end+"px":oFirst.style.top=end+"px";
			};
			return{a:a,v:v,flag:flag,end:end,here:here}
		}
		function getMove(obj){
			var move=obj.here+obj.flag*(obj.v-obj.a*fadeCount)*fadeCount;
			if((obj.flag>0&&move>obj.end)||(obj.flag<0&&move<obj.end)){
				move=obj.end;
				fadeCount=fadeStep;
			};
			return move;
		}
		function showItem(cur){
			if(opt.contentId&&!_imgs[cur]&&(isInit==false||(isInit==true&&opt.isLoadInit==true))){
				$loadImg(_cont[cur],opt.backAttr);
				_imgs[cur]=true;
			};
			if(opt.contentId){
				_cont[cur].style.display=="none"&&(_cont[cur].style.display="block");
				_cont[cur].style.visibility=="hidden"&&(_cont[cur].style.visibility="visible");
			};
			if(opt.titleId){
				$addClass(_tabs[cur],opt.className);
				_tabs[cur].style.display=="none"&&(_tabs[cur].style.display="block");
			};
			opt.func(cur);
		}
	}
};
function $loadImg(obj,attr){
	var attr=attr||"back_src",
		images=obj.getElementsByTagName("IMG");
	for(var i=0,len=images.length;i<len;i++){
		var oImg=images[i],
			src=oImg.getAttribute(attr);
		''==oImg.src&&src&&(oImg.src=src);
	}
}

PP.index.init();
